<?php

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

// name configurable
class plugin_dmath_test {

// -----------
// functionals	
// -----------
	function _regex_url_make($type) { return "#http(s?)://(.*)\.".type."#"; }  // construct a regex
	function _script_string_make($script_url) { return '<script src="'.$script_url.'"></script>'; }	// construct a <script> string with src mark
	function _stylesheet_string_make($stylesheet_url) { return '<link rel="stylesheet" href="'.$stylesheet_url.'"></link>'; } 	// construct a <link> string with rel mark set as "stylesheet" and href mark



// -------------------------
// real effect-ful functions
// -------------------------
	// pre-process the key parameters
	function common() {
		global $_G;
		$plugin_cache = $_G['cache']['plugin']['dmath_test'];  // set alias for cache
		$this->pluginurl = $_G['siteurl']."source/plugin/dmath_test/";	// get this plugin's location, configurable

		// inline default settings
		$this->katex = array(
			'stylesheet' => "https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css",
			'source' => "https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.js",
			'autorender' => "https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/contrib/auto-render.min.js",
			'allow_autorender' => false
		);

		// configure user settings
		foreach ([['css','stylesheet'], ['js','source'], ['js','autorender']] as $type) {
			if (empty($plugin_cache[type[1]])) {
				if (preg_match($this->_regex_url_make(type[0]), $plugin_cache[type[1]])) {
					// match successful, make user settings valid
					$this->katex[type[1]] = $plugin_cache[type[1]];
				}//if preg
			}//if empty
		}//foreach
	}//function



	// load katex configuration, stylesheet and script on critical pages
	function global_footer() {
		$script = '';
		
		// critical pages check
		if (CURMODULE == 'viewthread' || CURMODULE == 'forumdisplay' || CURMODULE == 'post' ) {
			// inline configuration
			// displayMode: symbols like \sum and \int will become large. Formulas will be centered in the line.
			$script .= "
			<script>
				var katex_options = {
					displayMode: true,
					output: \"html\"
				} 
			</script>";

			// stylesheet and js
			// the url has been set in $this->common()
			foreach(['stylesheet', 'source', 'autorender'] as $type) {
				$url = $this->katex[$type];
				if ($type=='stylesheet') $script .= $this->_stylesheet_string_make($url);
				else $script .= $this->_script_string_make($url);
			}//foreach
		}//if CURMODE

		// set parsing delimeters for thread-viewing
		if (CURMODULE=='viewthread') {
			$script .= "
			<script>
				document.addEventListener(\"DOMContentLoaded\", function() {
					renderMathInElement(document.body, {
						delimiters: [{left: \"\$\$\", right: \"\$\$\", display: true}]
					});
				});
			</script>";
		} else if (CURMODULE=='post') {
			$script .= "
			<style type=\"text/css\">
				.edt .b1r #dmath_editor {
					background:url(".$this->pluginurl."image/tex.png) no-repeat;background-position:center 1px;
				}
                .edt .b2r #dmath_editor {
                    background:url(".$this->pluginurl."image/tex_low.png) no-repeat;background-position:center 1px;
                }
			</style>
			";
		}	
		return $script;
	}
}





class plugin_dmath_test_forum extends plugin_dmath_test {

	// add latex button on the editor's control panel
	function post_editorctrl_left() {
		global $_G;
		$button_str = '<a';

		$button_configurations = [
			["id", "dmath_editor"],
			["menupos", "00"],
			["menuwidth", "600"],
			["title", ""],
			["menupos", "00"],
			["menuwidth", "600"],
			["onclick", "showWindow(\"katex_edit_window\", \"plugin.php?id=dmath_test:dmath&adds=e_iframe\");"],
		];

		foreach ($button_configurations as $properties) {
			$button_str .= " ".$properties[0]."="."'".$properties[1]."'";
		}

		$button_str .= ">公式</a>";

		// name configurable
		return $button_str;
	}
}

?>

